package com.chemical.search.util.page;

import java.util.ArrayList;
import java.util.List;


/**
 *@className:PageUtil.java
 *@classDescription:
 *@author:MChen
 *@createTime:2012-4-25
 */
public class PageUtil{

	/**
	 * 获取分页显示标签
	 * 
	 * @param page
	 *            page对象
	 * @return
	 */
	public static String getTag(Page page) {
		return getTagByGoogle(page);
	}
	/**
	 * 获取分页显示标签
	 * 
	 * @param page
	 *            page对象
	 * @return
	 */
	public static String getTagByGoogle(Page page) {
		StringBuffer tag=new StringBuffer();
		tag.append("  <a href='" + page.getForwordName()
				+ 1 + "'>首页</a>&nbsp;");
		
		tag.append("  <a href='" + page.getForwordName()
				+ page.getPrePage() + "'>上页</a>&nbsp;");
		
		//-----------
		int start=(page.getPageNo()-1);
		
		if(page.getPageNo()>page.getLeftCount()){
			start=page.getLeftCount();
		}	
		for(int i=start;i>0;i--){
			tag.append("  <a href='" + (page.getForwordName()+(page.getPageNo()-i)) + "'>"+(page.getPageNo()-i)+"</a>&nbsp;");
		}
		
		tag.append("  "+page.getPageNo()+"&nbsp;");
		
		//------------
		int end=(int)page.getTotalPages()-page.getPageNo();
		if(end>page.getRigthCount()){
			end=page.getRigthCount();
		}
		for(int i=1;i<=end;i++){
			tag.append("  <a href='" + (page.getForwordName()+(page.getPageNo()+i)) + "'>"+(page.getPageNo()+i)+"</a>&nbsp;");
		}
	
		
		tag.append("  <a href='" + page.getForwordName()
				+ page.getNextPage() + "'>下页</a>&nbsp;");
		tag.append("  <a href='" + page.getForwordName()
				+ page.getTotalPages() + "'>尾页</a>&nbsp;");
		
		tag.append("<br/>共" + page.getTotalCount() + "条  每页" + page.getPageSize() + "条  总共"
				+ page.getTotalPages() + "页 " + page.getPageNo() + "/" + page.getTotalPages() + "  ");
		
		return tag.toString();

	}
	/**
	 * 获取跳转的条件
	 * @param map
	 * @return
	 */
	public static  String getForwardCondition(List<Condition> list){
		StringBuffer fragment=new StringBuffer(getCondition(list,null));

		if("".equals(fragment.toString())){
			fragment.append("?pageNo=");
		}else{
			fragment.append("&pageNo=");
		}
		return fragment.toString();
	}
	/**
	 * 获取排序URL
	 * @param map
	 * @return
	 */
	public static  String getOrderCondition(List<Condition> list){
		
		StringBuffer fragment=new StringBuffer(getCondition(list,"order"));

		if("".equals(fragment.toString())){
			fragment.append("?order=");
		}else{
			fragment.append("&order=");
		}
		return fragment.toString();
	}
	/**
	 * 获取最基本的条件URL
	 * @param list
	 * @return
	 */
	public static String getCondition(List<Condition> list,String removeName){
		boolean flag=true;
		StringBuffer fragment=new StringBuffer("");
		for(Condition condition:list){
			if(!condition.getName().equals(removeName)){
				if(null!=condition.getValue()&&!"".equals(condition.getValue())){
					if(flag){
						fragment.append("?");
						flag=false;
					}else{
						fragment.append("&");
					}
					fragment.append(condition.getName()+"="+condition.getValue());
				}
			}
		}
		return fragment.toString();
	}
	/**
	 * 获取过滤条件查询
	 * @param list
	 * @return
	 */
	public static List<Condition> getFilterConditions(List<Condition> list){
		List<Condition> fragmentList=new ArrayList<Condition>();
		for(Condition condition:list){
			if(!"".equals(condition.getValue())&&null!=condition.getValue()){
				//暂时屏蔽排序显示
				if(condition.isFlag()){
					condition.setUrl(getCondition(list,condition.getName()));
					fragmentList.add(condition);
				}
			}
		}
		return fragmentList;
		
	}
}
